package org.leetcode.easy;

import org.junit.Test;

import java.util.Arrays;
import java.util.LinkedHashSet;

public class ThirdMax {
    public int thirdMax(int[] nums) {
        LinkedHashSet<Integer> set = new LinkedHashSet<>();
        for (int a :
                nums) {
            set.add(a);
        }
        Object[] arr = set.toArray();
        Arrays.sort(arr);
        if (arr.length == 2) {
            return Math.max((int) arr[0], (int) arr[1]);
        } else if (arr.length == 1) {
            return (int) arr[0];
        } else {
            return (int) arr[arr.length - 3];
        }
    }

    @Test
    public void test() {
        int[] nums = {1, 1, 2};
        System.out.println(thirdMax(nums));
    }
}
